package aimis.repo;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import aimis.domain.Department;
import aimis.domain.Role;
import aimis.domain.User;

import java.util.List;

//@Repository
public interface UserRepo extends JpaRepository<User, Long> {
    @Query("""
            select user from User as user where 
             :name is null or user.name like %:name%
            """)
    Page<User> findByQuery(String name, Pageable pageable);
    @Query("""
            select user from User as user where 
             (:name is null or user.name like %:name%) and (user.department.root=:root)
            """)
    Page<User> findByQuery(String name, Department root,Pageable pageable);
    @Query("""
            select user from User as user where 
            user.department.root=:root
            """)
    List<User> findAll(Department root);
    User findByLoginName(String loginName);

    @Query("""
            select user from User as user where 
             (user.department.root=:root)
            """)
    List<User> findByRoot(Department root);

    @Query("""
            select user from User as user where 
             (user.department.root=:root) and (:role member of user.roles)
            """)
    List<User> findByDepartmentAndRole(Department root,Role role);
}
